import mitt from 'mitt'
import { onMounted, onUnmounted } from 'vue'

export const eventBus = mitt()

export function useEventBus<T>(event: string | symbol, fn: (event: T) => void) {
  onMounted(() => {
    eventBus.on(event, fn as (event: unknown) => void)
  })
  onUnmounted(() => {
    eventBus.off(event, fn as (event: unknown) => void)
  })
}
